<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
    <el-form-item label="校区" prop="shopid">
      <el-select v-model="dataForm.shopid" placeholder= "校区"  @change="getCourse()">
        <el-option v-for="(item, index) in shopids"
                   :label="item.shopName"
                   :key="item.shopName"
                   :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="学员名字" prop="studentname">
      <el-input v-model="dataForm.studentname" placeholder="学员名字"></el-input>
    </el-form-item>
    <el-form-item label="学员生日" prop="studentage">
      <el-date-picker v-model="dataForm.studentage" type="date"  placeholder="学员生日" format="yyyy-MM-dd" value-format="yyyy-MM-dd" ></el-date-picker>
    </el-form-item>
      <el-form-item label="推荐人" prop="refereeName">
        <el-input v-model="dataForm.refereename" placeholder="推荐人"></el-input>
      </el-form-item>
    <el-form-item label="家长姓名" prop="studentmather">
      <el-input v-model="dataForm.studentmather" placeholder="家长姓名"></el-input>
    </el-form-item>
    <el-form-item label="家长电话" prop="matherphone">
      <el-input v-model="dataForm.matherphone" placeholder="家长电话"></el-input>
    </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="********"></el-input>
      </el-form-item>
    <!--
    <el-form-item label="应急联系人" prop="other">
      <el-input v-model="dataForm.other" placeholder="应急联系人"></el-input>
    </el-form-item>
    <el-form-item label="应急电话" prop="otherphone">
      <el-input v-model="dataForm.otherphone" placeholder="应急电话" type="number"></el-input>
    </el-form-item>
    -->
      <el-form-item label="家庭住址" prop="address">
        <el-input v-model="dataForm.address" placeholder="家庭住址"></el-input>
      </el-form-item>
      <el-form-item label="现就读学校" prop="school">
        <el-input v-model="dataForm.school" placeholder="现就读学校"></el-input>
      </el-form-item>
    <el-form-item label="会员卡类型" prop="cardtype" >
      <el-select v-model="dataForm.cardtype" placeholder= "会员卡类型"  @change = 'getMemberCardTime()' >
        <el-option v-for="(item, index) in payeeoption"
                   :label="item.cardName"
                   :key="item.cardName"
                   :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <!--
    <el-form-item label="会员卡号" prop="cardnum">
      <el-input v-model="dataForm.cardnum" placeholder="自动生成" readOnly="true"></el-input>
    </el-form-item>
    -->
      <el-form-item label="选择课程" prop="chooseCourse" >
        <el-select v-model="dataForm.chooseCourse" placeholder= "选择课程">
          <el-option v-for="(item, index) in chooseCourseList"
                     :label="item.name"
                     :key="item.name"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="到期时间" prop="duetime">
      <el-date-picker v-model="dataForm.duetime" type="datetime"  placeholder="到期时间"  value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"  v-bind:disabled="stuct">
      </el-date-picker>
    </el-form-item>
      <el-form-item label="课时" prop="residualclasshour">
        <el-input v-model="dataForm.residualclasshour" placeholder="课时" v-bind:disabled="stuct" type="number"></el-input>
      </el-form-item>
      <el-form-item label="单节扣课时" prop="classPeriod">
        <el-input v-model="dataForm.classPeriod" placeholder="单节扣课时" v-bind:disabled="stuct" type="number"></el-input>
      </el-form-item>
      <el-form-item label="每周限约课时" prop="weekLimit">
        <el-input v-model="dataForm.weekLimit" placeholder="每周限约课时" v-bind:disabled="stuct" type="number"></el-input>
      </el-form-item>
      <el-form-item label="装备" prop="giftequipment">
        <el-input v-model="dataForm.giftequipment" placeholder="装备"></el-input>
      </el-form-item>
    <el-form-item label="学员缴费" prop="payment">
      <el-input v-model="dataForm.payment" placeholder="输入金额" type="number"></el-input>
    </el-form-item>
      <el-form-item label="课程顾问" prop="consultant">
        <el-input v-model="dataForm.consultant" placeholder="课程顾问"></el-input>
      </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"  ></el-input>
    </el-form-item>
    <!--<el-form-item label="修改时间" prop="updatetime">-->
      <!--<el-input v-model="dataForm.updatetime" placeholder="修改时间"></el-input>-->
    <!--</el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  /* eslint-disable no-unused-vars */

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopid: '',
          studentname: '',
          studentage: '',
          studentmather: '',
          matherphone: '',
          other: '',
          otherphone: '',
          password: '',
          studentpic: '',
          residualintegral: '',
          cardtype: {'coursetimes': '', 'id': '', 'cardname': '', 'surplus': null, 'teachername': null},
          cardstatus: '',
          cardnum: '',
          giftequipment: '',
          residualclasshour: '',
          classPeriod: '',
          weekLimit: '',
          duetime: '',
          payment: '',
          remark: '',
          createtime: '',
          updatetime: '',
          cardtypeInfo: '',
          chooseCourse: '',
          chooseCoursetime: [],
          refereename: '',
          address: '',
          school: '',
          consultant: ''
        },
        stuct: false,
        payeeoption: '',
        chooseCourseList: '',
        chooseCoursetimes: '',
        shopids: '',
        males: [{'name': '男', 'id': 1}, {'name': '女', 'id': 0}],
        dataRule: {
          shopid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          studentname: [
            { required: true, message: '学生名字不能为空', trigger: 'blur' }
          ],
          studentmather: [
            { required: true, message: '家长姓名不能为空', trigger: 'blur' }
          ],
          matherphone: [
            { required: true, message: '家长电话不能为空', trigger: 'blur' }
          ],
          cardtype: [
            { required: true, message: '会员卡类型不能为空', trigger: 'blur' }
          ],
          giftequipment: [
            { required: true, message: '装备不能为空', trigger: 'blur' }
          ],
          chooseCourse: [
            { required: true, message: '课程不能为空', trigger: 'blur' }
          ],
          residualclasshour: [
            { required: true, message: '课时不能为空', trigger: 'blur' }
          ],
          classPeriod: [
            { required: true, message: '单节扣课时不能为空', trigger: 'blur' }
          ],
          weekLimit: [
            { required: true, message: '每周限约课时不能为空', trigger: 'blur' }
          ],
          duetime: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          payment: [
            { required: true, message: '学员缴费不能为空', trigger: 'blur' }
          // ],
          // remark: [
          //   { required: true, message: '备注不能为空', trigger: 'blur' }
          // ],
          // createtime: [
          //   { required: true, message: '新增时间不能为空', trigger: 'blur' }
          // ],
          // updatetime: [
          //   { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this.getMemberCard()
      this.getShopInfo()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopid = data.student.shopid
                this.dataForm.studentname = data.student.studentname
                this.dataForm.studentage = data.student.studentage
                this.dataForm.studentmather = data.student.studentmather
                this.dataForm.matherphone = data.student.matherphone
                this.dataForm.studentfather = data.student.studentfather
                this.dataForm.fatherphone = data.student.fatherphone
                this.dataForm.other = data.student.other
                this.dataForm.otherphone = data.student.otherphone
                this.dataForm.male = data.student.male
                this.dataForm.studentpic = data.student.studentPic
                this.dataForm.residualintegral = data.student.residualintegral
                this.dataForm.cardtype = data.student.cardtype
                this.dataForm.cardstatus = data.student.cardstatus
                this.dataForm.cardnum = data.student.cardnum
                this.dataForm.giftequipment = data.student.giftequipment
                this.dataForm.residualclasshour = data.student.residualclasshour
                this.dataForm.classPeriod = data.student.classPeriod
                this.dataForm.weekLimit = data.student.weekLimit
                this.dataForm.duetime = data.student.duetime
                this.dataForm.payment = data.student.payment
                this.dataForm.remark = data.student.remark
                this.dataForm.createtime = data.student.createtime
                this.dataForm.updatetime = data.student.updatetime
                this.dataForm.refereename = data.student.refereename
                this.dataForm.address = data.student.address
                this.dataForm.school = data.student.school
                this.dataForm.consultant = data.student.consultant
                this.getOneCourse()
                this.getTeacherInfo()
              }
            })
          } else {
            this.dataForm.chooseCoursetime = []
            this.dataForm.chooseCourse = ''
            this.dataForm.cardtype = ''
            this.stuct = false
          }
        })
      },
      // 获取老师的权限
      getTeacherInfo () {
        var userId = this.$store.state.user.id
        this.$http({
          url: this.$http.adornUrl('/business/student/queryTeacherLogin'),
          method: 'get',
          params: {'userId': userId}
        }).then(({data}) => {
          if (data.teacherInfo == null) {
            this.stuct = false
          } else {
            console.log(data)
            if (this.dataForm.cardtype === '' && this.dataForm.chooseCourse === '') {
              this.stuct = false
            } else {
              this.stuct = true
            }
          }
        })
      },
      // 查询会员卡状态
      getMemberCard () {
        this.$http({
          url: this.$http.adornUrl('/business/basemembercard/getMemberCard'),
          method: 'get',
          params: ''
        }).then(({data}) => {
          this.payeeoption = data
        })
      },
      // 查询会员卡课时
      getMemberCardTime () {
        let cardId = this.dataForm.cardtype
        let payeeoption = this.payeeoption
        for (var i = 0; i < payeeoption.length; i++) {
          if (cardId === payeeoption[i].id) {
            this.dataForm.residualclasshour = payeeoption[i].coursetimes
          }
        }
      },
      // 根据店铺，查询可选课程
      getCourse () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getCourse`),
          method: 'get',
          params: {'shopid': this.dataForm.shopid}
        }).then(({data}) => {
          this.chooseCourseList = data.courseEntityList
          this.dataForm.chooseCoursetime = []
          this.dataForm.chooseCourse = ''
        })
      },
      getCourse2 () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getCourse`),
          method: 'get',
          params: {'shopid': this.dataForm.shopid}
        }).then(({data}) => {
          this.chooseCourseList = data.courseEntityList
        })
      },
      // 查询学生所选课程
      getOneCourse () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getOneCourse`),
          method: 'get',
          params: {'id': this.dataForm.id}
        }).then(({data}) => {
          this.dataForm.chooseCourse = data.courseStudentEntity.courseId
          // this.getOneCourseTimes2()
          // this.getOneStudentCourseTimes()
          this.getCourse2()
        })
      },
      // 查询学生所选课程的包含的所有课程时间
      // getOneCourseTimes () {
      //   let courseid = this.dataForm.chooseCourse
      //   this.$http({
      //     url: this.$http.adornUrl(`/business/course/getCourseTimes`),
      //     method: 'get',
      //     params: {'id': courseid}
      //   }).then(({data}) => {
      //     this.chooseCoursetimes = data.courseTimes
      //     this.dataForm.chooseCoursetime = []
      //   })
      // },
      // 查询学生所选课程的包含的所有课程时间
      // getOneCourseTimes2 () {
      //   let courseid = this.dataForm.chooseCourse
      //   this.$http({
      //     url: this.$http.adornUrl(`/business/course/getCourseTimes`),
      //     method: 'get',
      //     params: {'id': courseid}
      //   }).then(({data}) => {
      //     this.chooseCoursetimes = data.courseTimes
      //   })
      // },
      // 查询学生选课时，已选择好的课程时间
      // getOneStudentCourseTimes () {
      //   let courseid = this.dataForm.chooseCourse
      //   let studentid = this.dataForm.id
      //   this.$http({
      //     url: this.$http.adornUrl(`/business/course/getOneStudentCourseTimes`),
      //     method: 'get',
      //     params: {'courseId': courseid, 'studentId': studentid}
      //   }).then(({data}) => {
      //     this.dataForm.chooseCoursetime = data.list
      //   })
      // },
      // 获取机构下所有店铺信息
      getShopInfo () {
        let trainId = '1'
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'trainId': trainId}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      // 限时数字，无小数点。
      handleInput (e) {
        var studentage = this.dataForm.studentage
        studentage = studentage.replace(/[^\d]/g, '')
        this.dataForm.studentage = studentage
      },
      // 时间格式转换方法
      dateFormat (time) {
        var date = new Date(time)
        var year = date.getFullYear()
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        // 拼接
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
      },
      dateFormat2 (time) {
        var date = new Date(time)
        var year = date.getFullYear()
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        // 拼接
        return year + '-' + month + '-' + day
      },
      check_num () {
        var studentage = this.dataForm.studentage
        studentage = studentage.replace(/[^\d]/g, '')
        this.dataForm.studentage = studentage
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (!this.dataForm.id) {
              if (!this.dataForm.password) {
                alert('请填写学生密码！')
                return
              }
            }
            if (this.dataForm.classPeriod < 1) {
              alert('单节扣课时必须大于0')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/business/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopid': this.dataForm.shopid,
                'studentname': this.dataForm.studentname,
                'studentage': this.dataForm.studentage,
                'studentmather': this.dataForm.studentmather,
                'matherphone': this.dataForm.matherphone,
                'studentfather': this.dataForm.studentfather,
                'fatherphone': this.dataForm.fatherphone,
                'other': this.dataForm.other,
                'otherphone': this.dataForm.otherphone,
                'male': this.dataForm.male,
                'studentpic': this.dataForm.studentpic,
                'residualintegral': this.dataForm.residualintegral,
                'cardtype': this.dataForm.cardtype,
                'cardstatus': this.dataForm.cardstatus,
                'cardnum': this.dataForm.cardnum,
                'giftequipment': this.dataForm.giftequipment,
                'residualclasshour': this.dataForm.residualclasshour,
                'classPeriod': this.dataForm.classPeriod,
                'weekLimit': this.dataForm.weekLimit,
                'duetime': this.dataForm.duetime,
                'payment': this.dataForm.payment,
                'remark': this.dataForm.remark,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime,
                'courseId': this.dataForm.chooseCourse,
                'refereename': this.dataForm.refereename,
                'address': this.dataForm.address,
                'school': this.dataForm.school,
                'consultant': this.dataForm.consultant,
                'password': this.dataForm.password
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
